<template>
  <span>{{cText}}</span>
</template>

<script>
import { querySysCode } from '@/api/sys/sys-code'

export default {
  name: 'CcTextSc',
  props: {
    type: {
      type: String,
      required: true
    },
    value: [String, Array]
  },
  methods: {
    loadSysCodeName () {
      this.cValue = this.value
      this.cText = ''
      if (typeof this.cValue === 'string') {
        this.expandSysCodeName(this.cValue)
      } else if (this.cValue instanceof Array) {
        this.cValue.forEach(item => {
          this.expandSysCodeName(item)
        })
      }
    },
    expandSysCodeName (value) {
      querySysCode({
        condition: [
          { property: 'codeType', value: this.type, type: 'EQ' },
          { property: 'codeValue', value: value, type: 'EQ' },
          { property: 'status', value: '0', type: 'EQ' }
        ]
      }).then(response => {
        if (response.data.success === true && response.data.statusCode === 200) {
          if (response.data.data.records.length === 1) {
            this.cText += (this.cText ? '、' : '') + response.data.data.records[0].codeName
          }
        } else {
          this.$message({ type: 'error', message: response.data })
        }
      })
    }
  },
  watch: {
    value (val, oldVal) {
      this.loadSysCodeName()
    }
  },
  data () {
    return {
      cValue: [String, Array],
      cText: String
    }
  },
  mounted () {
    this.loadSysCodeName()
  }
}
</script>
